package edu.nf.vac.admin.controller;

import edu.nf.vac.admin.commons.Const;
import edu.nf.vac.admin.vo.ResultVO;
import edu.nf.vac.auth.inf.UserAuthService;
import edu.nf.vac.users.dto.PageDTO;
import edu.nf.vac.users.dto.UserDTO;
import edu.nf.vac.users.inf.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author : QYM
 * @create 2022/4/2
 */
@RestController
public class UserController extends BaseController{
    /**
     * 注入用户
     */
    @DubboReference(interfaceClass = UserService.class,version = "1.0.0")
    private UserService userService;

    /**
     * 注入认证
     */
    @DubboReference(interfaceClass = UserAuthService.class,version = "1.0.0")
    private UserAuthService authService;

    /**
     * 查询用户列表
     * @param userDTO
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/users/list")
    public ResultVO<List<UserDTO>> listUser(UserDTO userDTO,int page,int limit){
        PageDTO<UserDTO> pageDTO = userService.listUser(userDTO, page, limit);
        return pageResult(pageDTO.getTotal(),pageDTO.getList());
    }

    /**
     * 根据用户ID查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public ResultVO<UserDTO> getUserById(@PathVariable("id") int id){
        UserDTO dto = userService.getUserById(id);
        return success(dto);
    }

    /**
     * 添加用户
     * @param userDTO
     * @return
     */
    @PostMapping("/users/save")
    public ResultVO addUser(UserDTO userDTO){
        userService.addUser(userDTO);
        return success();
    }

    /**
     * 修改用户
     * @param userDTO
     * @return
     */
    @PostMapping("/users/update")
    public ResultVO updateUser(UserDTO userDTO){
        userService.updateUser(userDTO);
        return success();
    }

    /**
     * 后台管理系统登录
     * @param tel
     * @param password
     * @param session
     * @return
     */
    @PostMapping("/users/login")
    public ResultVO adminLogin(String tel, String password, HttpSession session){
        UserDTO user = authService.authenticate(tel, password);
        session.setAttribute(Const.CURRENT_USER,user);
        return success("home.html");
    }

    /**
     * 退出当前登录
     * @param session
     * @param response
     * @throws IOException
     */
    @GetMapping("/users/logout")
    public void logout(HttpSession session, HttpServletResponse response) throws IOException {
        session.invalidate();
        response.sendRedirect("../login.html");
    }

    /**
     * 修改密码
     * @param session
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PostMapping("/users/changePwd")
    public ResultVO changePwd(HttpSession session,String oldPassword,String newPassword){
        UserDTO user = (UserDTO) session.getAttribute(Const.CURRENT_USER);
        userService.changePwd(user,oldPassword,newPassword);
        session.invalidate();
        return success("修改密码成功");
    }

    /**
     * 修改用户状态
     * @param dto
     * @return
     */
    @PostMapping("/users/changeUserStatus")
    public ResultVO changeUserStatus(UserDTO dto){
        userService.changeUserStatus(dto.getUid(),dto.getUserStatus());
        return success();
    }
}
